/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servletClass;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sessionbean.CMSClassFacadeLocal;
import sessionbean.CourseFacadeLocal;
import supportlearning.entities.CMSClass;
import supportlearning.entities.Course;

/**
 *
 * @author Huy
 */
public class manageClass extends HttpServlet {

    @EJB
    private CourseFacadeLocal courseFacade;
    @EJB
    private CMSClassFacadeLocal cMSClassFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            if (action == null) {
                List<CMSClass> listclass = cMSClassFacade.findAll();
                request.setAttribute("listclass", listclass);
                request.getRequestDispatcher("manageClass.jsp").forward(request, response);
            } else if (action.equals("linkid")) {
                int id = Integer.parseInt(request.getParameter("id"));
                CMSClass classobj = cMSClassFacade.find(id);
                List<Course> listcourse = courseFacade.findAll();
                request.setAttribute("classobj", classobj);
                request.setAttribute("listcourse", listcourse);
                request.getRequestDispatcher("updateClass.jsp").forward(request, response);
            } else if (action.equals("Update")) {
                int id = Integer.parseInt(request.getParameter("txtclassid"));
                String classname = request.getParameter("txtclassname");
                String descript = request.getParameter("descourse");
                int courseid = Integer.parseInt(request.getParameter("selectcoursename"));
                Course courseobj = courseFacade.find(courseid);
                CMSClass classobj = cMSClassFacade.find(id);
                classobj.setClassName(classname);
                classobj.setDescription(descript);
                classobj.setCourse(courseobj);
                cMSClassFacade.edit(classobj);
                response.sendRedirect("updateclasssuccess.jsp");
            } else if (action.equals("createclass")) {
                List<Course> listcourse = courseFacade.findAll();

                request.setAttribute("listcourse", listcourse);
                request.getRequestDispatcher("createClass.jsp").forward(request, response);
            } else if (action.equals("Create")) {
                String classname = request.getParameter("txtclassname");
                String descript = request.getParameter("descourse");
                int courseid = Integer.parseInt(request.getParameter("selectcoursename"));
                Course newco = courseFacade.find(courseid);
                CMSClass classobj = new CMSClass();
                classobj.setClassName(classname);
                classobj.setDescription(descript);
                classobj.setCourse(newco);
                cMSClassFacade.create(classobj);
                response.sendRedirect("createclasssuccessfull.jsp");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
